package com.peking.donations.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.peking.donations.model.DonationSaving;

/**
 * Created by wyz on 16/4/27.
 */
@Repository
public interface DonationSavingDao extends CrudRepository<DonationSaving, Integer> {

    DonationSaving findByTransactionId(String transactionId);

    @Query("select ds from DonationSaving as ds where orderId=?1 and savingStatus=1")
    List<DonationSaving> findByOrderId(int orderId);

    @Query("select ds from DonationSaving as ds where userId=?1 and savingStatus=1")
    List<DonationSaving> findByUserId(int userId);

    @Query("select sum(ds.share) from DonationSaving as ds where userId=?1 and savingStatus=1")
    int accountUserShare(int userId);

    @Query("select sum(ds.share) from DonationSaving as ds where orderId=?1 and savingStatus=1")
    int accountCallOrderShare(int orderId);

    @Query("select count(distinct  ds.userId) from DonationSaving as ds where orderId=?1 and savingStatus=1")
    int countCallOrderJoins(Integer gcId);
}
